package com.example.bili.controller;

import com.example.bili.entity.User;
import com.example.bili.service.UserService;
import com.example.bili.util.CommonUtil;
import com.example.bili.util.RedisUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/user")
@CrossOrigin(origins = "http://localhost:5173")
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private RedisUtil redisUtil;

    @PostMapping("/login")
    public Object login(@RequestParam String uPhone, @RequestParam String uPwd) {
        // 登录
        User logUser = userService.login(uPhone,uPwd);
        // 存储
        if (logUser != null) {
            redisUtil.set(CommonUtil.DEV_USER_KEY, logUser);
            return logUser;
        }
        return "登录失败";
    }

    // 注销
    @GetMapping("/logout")
    public Object logout() {
        redisUtil.set(CommonUtil.DEV_USER_KEY,null);
        return true;
    }

    //查询
    @GetMapping("/queryById")
    public Object queryById(@RequestParam String uPhone) {
        User user = userService.queryById(uPhone);
        if (user != null) {
            return ResponseEntity.ok(user);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    //查询收藏
    @GetMapping("/foover")
    public List<User> foover(@RequestParam String uPhone) {
        return userService.foover(uPhone);
    }

    //修改
    @PostMapping("/upUser")
    public boolean upUser(@RequestParam("uPhone") String uPhone,
                          @RequestParam("uName") String uName,
                          @RequestParam("uSex") String uSex,
                          @RequestParam("birthday")  @DateTimeFormat(pattern = "yyyy-MM-dd") Date birthday,
                          @RequestParam("motto") String motto,
                          @RequestParam(value = "uAvatarurl", required = false) MultipartFile uAvatarurl) {
        User user = new User();
        user.setUphone(uPhone);
        user.setUname(uName);
        user.setUsex(uSex);
        user.setBirthday(birthday);
        user.setMotto(motto);

        if (uAvatarurl != null && !uAvatarurl.isEmpty()) {
            try {
                // 保存文件到指定目录
                String uploadDir = "D:/代码/biliqian/public/";
                String fileName = "uesrimgs/" + uAvatarurl.getOriginalFilename();
                File dest = new File(uploadDir + fileName);
                if (!dest.getParentFile().exists()) {
                    dest.getParentFile().mkdirs();
                }
                uAvatarurl.transferTo(dest);
                user.setUavatarurl(fileName);
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }

        boolean upUsers = userService.upUser(user);
        return upUsers;
    }


    @GetMapping("/getAllUsers")
    public List<User> getAllUsers(@RequestParam Integer id,@RequestParam(required = false) String uName) {
        List<User> allUsers = userService.getAllUsers(id,uName);
        return allUsers;
    }


    @GetMapping("/getAllFUsers")
    public List<User> getAllFUsers(@RequestParam Integer id) {
        List<User> allUsers = userService.getAllFUsers(id);
        return allUsers;
    }

    @GetMapping("/deleteAllUsers")
    public boolean deleteAllUsers(@RequestParam Integer id) {
        boolean deleteAllUsers = userService.deleteAllUsers(id);
        return deleteAllUsers;
    }

    @GetMapping("/fover")
    public List<User> fover(@RequestParam String uPhone) {
        List<User> fover = userService.fover(uPhone);
        return fover;
    }


    @GetMapping("/fovere")
    public List<User> fovere(@RequestParam String uPhone) {
        List<User> fovere = userService.fovere(uPhone);
        return fovere;
    }

    @GetMapping("/fooover")
    public List<User> fooover(@RequestParam Integer feid) {
        List<User> fooover = userService.fooover(feid);
        return fooover;
    }

    @GetMapping("/foverr")
    public List<User> foverr(@RequestParam Integer feid) {
        List<User> foverr = userService.foverr(feid);
        return foverr;
    }

    @GetMapping("/getUser")
    public User getUser(@RequestParam Integer id) {
        User user = userService.getUser(id);
        return user;
    }

    @GetMapping("/upDateUser")
    public boolean upDateUser(@RequestParam Integer id) {
        boolean upDateUser = userService.upDateUser(id);
        return upDateUser;
    }

    @PostMapping("/insertUser")
    public boolean insertUser(@RequestParam("uPhone") String uPhone,
                              @RequestParam("uPwd") String uPwd,
                              @RequestParam(value = "uAvatarurl", required = false) MultipartFile uAvatarurl
    ){
        Random random = new Random();
        int randomUId = random.nextInt(10000);
        String name = "默认用户";
        String mottos = "这个人很懒，什么都没有留下";
        User user = new User();
        user.setUphone(uPhone);
        user.setUpwd(uPwd);
        user.setUid(randomUId);
        user.setUname(name);
        user.setMotto(mottos);
        user.setUavatarurl("/img/gg2.gif");
       return userService.insertUser(user);
    }


    @GetMapping("/listUser/{id}")
    public Object listUser(@PathVariable int id){
        return userService.listUser(id);
    }
    @GetMapping("/listUserZi/{id}")
    public Object listUserZi(@PathVariable int id,
                             @RequestParam(required = false,defaultValue = "0")int start,
                             @RequestParam(required = false,defaultValue = "4")int pageSize){
        return userService.listUserZi(id,start,pageSize);
    }
}
